home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / shells / kiss-0.11 / kiss-0 / kiss / src / runchild.c < prev    next >
C/C++ Source or Header  |  1995-03-23  |  2KB  |  77 lines

  1. #include "kiss.h"
  2.  
  3. /* for debugging purposes: */
  4. #define DUMPCMD(str,stack) {flags.debug = 1; dumpstack (str,stack);}
  5.  
  6. void runchild (Stringstack s, int background)
  7. {
  8.     register int
  9.     i,
  10.     j,
  11.     childpid;
  12.     Stringstack
  13.     one;
  14.     int
  15.     pipefound = 1,
  16.     p [2],
  17.     infile = STDIN_FILENO,
  18.     outfile = STDOUT_FILENO,
  19.     allow_inpipe = 1;
  20.  
  21.     /* set group id if necessary */
  22.     if (background && ! flags.controlkids)
  23.     setpgid (getpid (), getpid ());
  24.  
  25.     /* parse from left to right, splitting into piped parts */
  26.     while (pipefound)
  27.     {
  28.     pipefound = 0;                /* assume no pipe present */
  29.     for (i = 1; i < s.nstr - 1; i++)
  30.         if (! strcmp (s.str [i], "|"))
  31.         {
  32.         pipefound = 1;
  33.         /* found pipe: copy that part to 'one'*/
  34.         one.str = xmalloc ( i * sizeof (char *) );
  35.         one.nstr = i;
  36.         for (j = 0; j < i; j++)
  37.             one.str [j] = s.str [j];
  38.         /* remove that part from 's' */
  39.         s.nstr -= i + 1;
  40.         s.str  += i + 1;
  41.  
  42.         if (pipe (p) < 0)
  43.         {
  44.             warning ("pipe failure");
  45.             return;
  46.         }
  47.         outfile = p [P_BLOW];
  48.  
  49.         if ( (childpid = fork ()) < 0 )        /* fork failure? */
  50.         {
  51.             warning ("fork failure");
  52.             return;
  53.         }
  54.         else if (! childpid)            /* child process */
  55.         {
  56.             setshlvl ();
  57.             onechild (one, infile, outfile, allow_inpipe, 0);
  58.         }
  59.         else                    /* parent */
  60.         {
  61.             allow_inpipe = 0;            /* next may not pipe in */
  62.         
  63.             if (infile != STDIN_FILENO)        /* clean up pipes */
  64.             close (infile);
  65.             if (outfile != STDOUT_FILENO)
  66.             close (outfile);
  67.         
  68.             infile = p [P_SUCK];        /* infile for next one */
  69.         }
  70.         break;                    /* terminate for loop, */
  71.         }                        /* fall thru to while */
  72.     }
  73.  
  74.     /* here's the last one */
  75.     onechild (s, infile, STDOUT_FILENO, allow_inpipe, 1);
  76. }
  77.